package com.gongjian.web.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gongjian.database.entity.Register;
import com.gongjian.database.entity.User;
import com.gongjian.database.service.RegisterService;
import com.gongjian.utile.CodeEntity;
import com.gongjian.utile.Ids;
import com.gongjian.utile.utile.Hump;
import com.sun.org.apache.regexp.internal.RE;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * @Author: TanLiLin
 * @Date: 2019/3/23 15:30
 * @Version 1.0
 */
@RestController
public class RegisterController {

    @Autowired
    RegisterService registerService;
    @Autowired
    SqlSession sqlSession;

    /**
     * 查询所有记录
     * @param
     * @return
     */
    @RequestMapping("/register/findAllRegister")
    public String addStuUser(){
        return registerService.findAllRegister(1,10000000);
    }
 @RequestMapping("/register/findAllRegisterByUserId")
    public String findAllRegisterByUserId(HttpSession httpSession){
     User user=(User)httpSession.getAttribute("user");
             List<Map<String,Object>> registerList = sqlSession.selectList("findAllRegisterByUserId",user.getUserId());
             registerList= Hump.formatHumpNameForList(registerList);
             CodeEntity codeEntity = new CodeEntity(200,"ok");
             String timeFormat = "yyyy-MM-dd HH:mm:ss";
             JSONArray jsonArray = JSONArray.parseArray(JSONObject.toJSONStringWithDateFormat(registerList,timeFormat));  /**list为 List<Map<String, String>>**/

             codeEntity.setData(jsonArray);

        return codeEntity.toJSONString();
    }

    /**
     * 通过registerId查询一条记录
     * @param registerId
     * @return
     */
    @RequestMapping("/register/findRegisterByRegisterId")
    public String findRegisterByRegisterId(int registerId){
        return registerService.findRegisterByRegisterId(registerId);
    }


    /**
     * 多条记录删除
     * @param ids
     * @return
     */
    @RequestMapping("/register/deleteRegisterByRegisterId")
    public String deleteRegisterByRegisterId(@RequestBody Ids ids){
        return registerService.deleteRegisterByRegisterId(ids.getIds());
    }

    /**
     * 根据registerId修改记录
     * @param
     * @return
     */
    @RequestMapping("/register/updateRegister")
    public String updateRegister(Register register) {
        System.out.println(register.getRegisterState()+register.getRegisterId());
        return registerService.updateRegister(register);
    }

    /**
     * 添加一条记录
     * @param register
     * @return
     */
    @RequestMapping("/register/addRegister")
    public String addRegister(@RequestBody  Register register){
        Date date =new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        register.setRegisterTime(timestamp);
        return registerService.addRegister(register);
    }

    /**
     * 获取签到信息 根据ID
     */
    @RequestMapping("/register/getAllRegise")
    public String getAllRegise(int activityId){

        return  "";
    }




}

